package com.service.impl;

import com.beans.Admin;
import com.beans.Forum;
import com.beans.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vo.ForumVo;
import com.mapper.ForumMapper;
import com.service.ForumService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service
public class ForumServiceImpl implements ForumService {

    @Resource
    private ForumMapper forumMapper;

    @Override
    public List<ForumVo> getAllForum() {
        return forumMapper.allForum();
    }

    @Override
    public int addForum(Forum forum) {
        return forumMapper.addForum(forum);
    }

    @Override
    public ForumVo selectForumById(int id) {
        return forumMapper.selectForumById(id);
    }

    @Override
    public int deleteForum(int id) {
        return forumMapper.deleteForum(id);
    }

    @Override
    public int updateForum(Forum forum) {
        return forumMapper.updateForum(forum);
    }

    @Override
    public List<ForumVo> selectByTitle(String title) {
        return forumMapper.selectByTitle(title);
    }

    @Override
    public Map<String, Object> queryByPage(Forum forum, Integer page, Integer limit) {
        Map<String, Object> resMap = new HashMap<>(4);
        PageHelper.offsetPage((page - 1) * limit, limit);
        List<ForumVo> dataList = Collections.emptyList();
        if(StringUtils.hasText(forum.getTitle())){
            dataList = forumMapper.selectByTitle(forum.getTitle());
        }else{
            dataList = forumMapper.allForum();
        }
        PageInfo<Forum> pageInfo = new PageInfo<>(dataList);
        resMap.put("code", 0);
        resMap.put("msg", "查询成功");
        resMap.put("data", pageInfo.getList());
        resMap.put("count", pageInfo.getTotal());
        return resMap;
    }
}
